﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.Serialization;

namespace AMSService.model
{
    [DataContract (IsReference=true)]//(Namespace="AMSService.model")]
    public class WarrantyData : DBConnector
    {
        private int _id = 0;
        private DateTime _start;
        private DateTime _end;
        private string _type;
        private string _servno;
        private string _comments;
        private Asset _asset;
        private List<AMSImage> _images;

        // Stored procedure names
        private const string _spgetwarrbyid = "GetWarrantyById";
        private const string _spdelwarrbyid = "DeleteWarrantyById";
        private const string _spinswarr = "InsertWarranty";
        private const string _spupdwarr = "UpdateWarranty";

        // Stored procedure arguments
        private const string _prmid = "@id";
        private const string _prmarg = "@arg";
        private const string _prmstart = "@startdate";
        private const string _prmend = "@enddate";
        private const string _prmwarrtype = "@warrtype";
        private const string _prmservnum = "@servnum";
        private const string _prmcomm = "@comments";
        private const string _prmassetid = "@assetid";

        // Table field names
        private const string _fldid = "pkid";
        private const string _fldstart = "startdate";
        private const string _fldend = "enddate";
        private const string _fldtype = "warrtype";
        private const string _fldservno = "servnum";
        private const string _fldcomm = "comments";
        private const string _fldassetid = "fkassetid";

        public enum _spargs { id, assetid };

        public WarrantyData(Asset BaseItem)
        {
            _asset = BaseItem;
            _id = 0;
            InitializeAttributes();
        }

        public WarrantyData(Asset BaseItem, int ID)
        {
            _asset = BaseItem;
            InitializeAttributes();
            FillWarrData(ID);
        }

        public WarrantyData(Asset BaseItem, DataRow Row)
        {
            _asset = BaseItem;
            InitializeAttributes();
            FillWarrData(Row);
        }

        public static string SpSelect
        {
            get { return _spgetwarrbyid; }
        }

        public static string SpPrmId
        {
            get { return _prmid; }
        }

        public static string SpPrmArg
        {
            get { return _prmarg; }
        }

        [DataMember]
        public int ID
        {
            get { return _id; }
            set { _id = value; }
        }

        [DataMember]
        public DateTime Start
        {
            get { return _start; }
            set { _start = value; }
        }

        [DataMember]
        public DateTime End
        {
            get { return _end; }
            set { _end = value; }
        }

        [DataMember]
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        [DataMember]
        public string ServiceNumber
        {
            get { return _servno; }
            set { _servno = value; }
        }

        [DataMember]
        public string Comments
        {
            get { return _comments; }
            set { _comments = value; }
        }

        [DataMember]
        public Asset BaseItem
        {
            get { return _asset; }
            set { _asset = value; }
        }

        [DataMember]
        public List<AMSImage> Images
        {
            get { return _images; }
            private set { }
        }

        public bool Delete()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            paramlist.Add(pid);

            return SendWriteRequest(_spdelwarrbyid, paramlist) > 0;
        }

        private void FillWarrData(DataRow dr)
        {
            _id = Convert.ToInt32(dr[_fldid].ToString());
            _start = Convert.ToDateTime(dr[_fldstart].ToString());
            _end = Convert.ToDateTime(dr[_fldend].ToString());
            _type = dr[_fldtype].ToString();
            _servno = dr[_fldservno].ToString();
            _comments = dr[_fldcomm].ToString();
        }

        private void FillWarrData(int ID)
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter parg = new SqlParameter(_prmarg, (int)_spargs.id);
            paramlist.Add(pid);
            paramlist.Add(parg);
            DataTable dt = SendReadRequest(_spgetwarrbyid, paramlist);

            if (dt.Rows.Count > 0)
            {
                FillWarrData(dt.Rows[0]);
            }
        }

        private void InitializeAttributes()
        {
            _images = new List<AMSImage>();
        }

        public void LoadImages()
        {
            DataTable dt = SendReadRequest(AMSImage.SpSelect,
                new SqlParameter[] { new SqlParameter(AMSImage.SpPrmId, _id),
                                     new SqlParameter(AMSImage.SpPrmArg, AMSImage._spargs.warrid) });

            if (_images == null)
                _images = new List<AMSImage>();
            else
                _images.Clear();

            foreach (DataRow dr in dt.Rows)
                _images.Add(new AMSImage(_asset, this, dr));
        }

        public bool Save()
        {
            List<SqlParameter> paramlist = new List<SqlParameter>();
            SqlParameter pid = new SqlParameter(_prmid, _id);
            SqlParameter pstart = new SqlParameter(_prmstart, _start);
            SqlParameter pend = new SqlParameter(_prmend, _end);
            SqlParameter ptype = new SqlParameter(_prmwarrtype, _type);
            SqlParameter pservno = new SqlParameter(_prmservnum, _servno);
            SqlParameter pcomm = new SqlParameter(_prmcomm, _comments);
            SqlParameter passetid = new SqlParameter(_prmassetid, _asset.ID);

            paramlist.Add(pstart);
            paramlist.Add(pend);
            paramlist.Add(ptype);
            paramlist.Add(pservno);
            paramlist.Add(pcomm);
            paramlist.Add(passetid);

            // If this is an update request (the id already exists)
            if (_id > 0)
            {
                paramlist.Add(pid);
                return SendWriteRequest(_spupdwarr, paramlist) > 0;
            }
            else
            {
                _id = SendWriteRequest(_spinswarr, paramlist);
                return _id > 0;
            }
        }
    }
}